/** @format */

import { getToday } from './utils'

// login初始化用户信息
export const initCustomUserWhenLogin = (context: IContext<IParams>, activityInfo?: IActivityInfo): ICustomUserInfo => {
  const { todaySendBlessingTimes, todayDrawLotsTimes } = context.data
  return {
    luckPoint: 0,
    rankLuckPoint: 0, // 排行榜福气值，限12.31 22点 之后不进行统计
    drawLotsTimes: 0,
    sendBlessingTimes:  0,
    todayDrawLotsTimes: +todayDrawLotsTimes,
    todaySendBlessingTimes: +todaySendBlessingTimes,
    todayLotTaskCompleteTimes: 0,
    todayBlessTaskCompleteTimes: 0,
    lotFollowSellerIds: [],
    blessFollowSellerIds: [],
  }
}

// login接口 更新用户时， 多用于当天第一次登陆的逻辑
export const updateUserWhenLogin = (
  context: IContext<IParams>,
  userInfo: IUserInfo
): IUpdateQuery => {
  const today = getToday()
  const { todaySendBlessingTimes, todayDrawLotsTimes } = context.data
  const firstLoginToday = !userInfo?.login?.[today]
  const loginTodays = Object.keys(userInfo?.login)?.length || 0;
  const isLimit = loginTodays >= 8
  let set = {}
  if (firstLoginToday) {
    set = {
      todayLotTaskCompleteTimes: 0,
      todayBlessTaskCompleteTimes: 0,
    }
    if (!isLimit) {
      set = {
        ...set,
        todayDrawLotsTimes: +todayDrawLotsTimes,
        todaySendBlessingTimes: +todaySendBlessingTimes,
      }
    }
  }
  return {
    $set: set
  }
}
